package com.example.demo.controller;

import com.example.demo.controller.requests.ModifyStudentRequest;
import com.example.demo.model.Student;
import com.example.demo.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    public IStudentService iStudentService;

    //    查询所有学生信息
    @GetMapping("/stu/all")
    public List <Student> getAllStudent(){
        List <Student> allStudent=iStudentService.getAllStudents();
        return allStudent;
    }

//    新增学生信息
    @PostMapping("/stu/add")
    public Student addNewStudent(@RequestBody Student student){
        log.info("新增学生信息：{}",student);

        int StudentID=student.getId();
        if (iStudentService.getStudentById(StudentID) != null){
            return null;
        }else {
            iStudentService.addNewStudent(student);
            return student;
        }
    }

    //查询单个学生信息，据id号
    @GetMapping("/stu/{id}")
    public Student getStudent(@PathVariable("id") Integer id){
        log.info("要查询的学生id：{}",id);

        if (iStudentService.getStudentById(id)!=null){
            return iStudentService.getStudentById(id);
        }else {
            return null;
        }
//        Student student=Student.builder().id(1)
//                .name("罗永生")
//                .adminDate(LocalDate.of(2011,2,22))
//                .major("软件技术")
//                .build();
//        log.info("要查询的学生信息：{}",student);
//        return student;
    }

//    修改更新学生信息
    @PutMapping("/stu/update")
    public Student updateStudent(@RequestBody ModifyStudentRequest request) {
        log.info("要修改的学生信息：{}",request);
//        1. 查看有无此学生
        Student student=iStudentService.getStudentById(request.getId());
//        2.把要修改的内容=》drink对应属性
        BeanUtils.copyProperties(request,student);
//        3.更新数据库
        iStudentService.updateStudent(student);
        return student;
    }

    //    删除学生信息
    @DeleteMapping("/stu/del/{id}")
    public String deleteStudent(@PathVariable Integer id) {
        log.info("要删除的学生id: {}",id);
        iStudentService.deleteStudent(id);
        return "删除学生信息成功";
    }

//    文件上传
//    @PostMapping(value = "/stu/uploadFile",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
//    public List<Student> uploadFile(MultipartFile file){
//        List<Student> results=new ArrayList<>();
//        try {
//            BufferedReader reader=new BufferedReader(new InputStreamReader(file.getInputStream()));
//            String line="";
//            while ((line=reader.readLine())!=null){
//                String[] args=line.split(",");
//                results.add(Student.builder().id(Integer.parseInt(args[0]))
//                        .name(args[1])
//                        .adminDate(LocalDate.parse(args[2]))
//                        .major(args[3])
//                        .build());
//            }
//        }catch (IOException e){
//            e.printStackTrace();
//        }
//        return results;
//
//    }
}
